package com.africanmoviesmobile.webservice;

import java.io.IOException;
import java.util.Hashtable;

import org.json.JSONArray;
import org.json.JSONObject;
import org.pixmob.httpclient.HttpClientException;

import android.content.Context;

import com.africanmoviesmobile.webservice.inf.Cast;
import com.africanmoviesmobile.webservice.inf.Crew;
import com.africanmoviesmobile.webservice.inf.Movie;
public class AmmMovie extends AmmWebService implements Movie {

	private JSONObject _jsonObject;

	public AmmMovie(JSONObject jsonObject, Context context) {
		super(context);
		_jsonObject = jsonObject;
	}

	public int getId() {
		return _jsonObject.optInt("movie_id");
	}

	public String getTitle() {
		return _jsonObject.optString("title");
	}

	public String getSynopsis() {
		return _jsonObject.optString("synopsis");
	}

	public String getRunTime() {
		return _jsonObject.optString("run_time");
	}

	public String getReleaseDate() {
		return _jsonObject.optString("release_date");
	}

	public String getMpaaRating() {
		return _jsonObject.optString("mpaa_rating");
	}

	public String getLanguage() {
		return _jsonObject.optString("language");
	}
	
	public String getCountry() {
		return _jsonObject.optString("country");
	}

	public String getCover() {
		return _jsonObject.optString("cover_url");
	}
	
	public String getMobileScreenshot() {
		return _jsonObject.optString("mobile_screenshot_url");
	}
	
	public String getGenres() {
		return _jsonObject.optString("genres");
	}
	
	public String getShortCast() {
		return _jsonObject.optString("short_cast");
	}

	public Cast[] getFullCast() {

		Cast[] result = null;

		JSONArray ja = _jsonObject.optJSONArray("full_cast");

		if ((ja != null) && (ja.length() > 0)) {

			result = new Cast[ja.length()];

			for (int i = 0; i < ja.length(); i++) {
				result[i] = new AmmCast(ja.optJSONObject(i), getContext());
			}
		}

		return result;
	}

	public Crew[] getWriters() {
		
		Crew[] result = null;

		JSONArray ja = _jsonObject.optJSONArray("writers");

		if ((ja != null) && (ja.length() > 0)) {

			result = new Crew[ja.length()];

			for (int i = 0; i < ja.length(); i++) {
				result[i] = new AmmCrew(ja.optJSONObject(i), getContext());
			}
		}

		return result;
	}

	public Crew[] getProducers() {


		Crew[] result = null;

		JSONArray ja = _jsonObject.optJSONArray("producers");

		if ((ja != null) && (ja.length() > 0)) {

			result = new Crew[ja.length()];

			for (int i = 0; i < ja.length(); i++) {
				result[i] = new AmmCrew(ja.optJSONObject(i), getContext());
			}
		}

		return result;
	}

	public Crew[] getDirectors() {

		Crew[] result = null;

		JSONArray ja = _jsonObject.optJSONArray("directors");

		if ((ja != null) && (ja.length() > 0)) {

			result = new Crew[ja.length()];

			for (int i = 0; i < ja.length(); i++) {
				result[i] = new AmmCrew(ja.optJSONObject(i), getContext());
			}
		}

		return result;
	}

	public String getCdnUrl() {
		return _jsonObject.optString("cdn_url");
	}

	public String getPreviewCdnUrl() {
		return _jsonObject.optString("preview_cdn_url");
	}
	
	public int getRatings(){
		return _jsonObject.optInt("ratings");
	}

	public int getRatingsCount(){
		return _jsonObject.optInt("ratings_count");
	}
	
	public int getRatingsSync() throws HttpClientException, IOException, WebServiceException{

		int result = 0;

		Hashtable<String, String> params = new Hashtable<String, String>();
		params.put("movieId", Integer.toString(getId()));

		try {

			JSONObject jo = read("movie/ratings", params);

			if ((jo != null) && (jo.length() > 0)) {
				if ((jo.has("ratings"))) {
					result = jo.optInt("ratings");
				}
			}

		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return result;

	}

	public void getRatings(final AsyncCallback listener, final Object state) {

		new Thread() {

			public void run() {

				try {

					int[] result = new int[1];
					result[0] = getRatingsSync();

					try {
						listener.onComplete(result, state);
					} catch (Throwable t) {
						t.printStackTrace();
					}
				} catch (HttpClientException e) {
					listener.onHttpClientException(e, state);
				} catch (IOException e) {
					listener.onIOException(e, state);
				} catch (WebServiceException e) {
					listener.onWebServiceException(e, state);
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}.start();

	}

	public int getRatingsCountSync() throws HttpClientException, IOException, WebServiceException{

		int result = 0;

		Hashtable<String, String> params = new Hashtable<String, String>();
		params.put("movieId", Integer.toString(getId()));

		try {

			JSONObject jo = read("movie/ratings", params);

			if ((jo != null) && (jo.length() > 0)) {
				if ((jo.has("ratings_count"))) {
					result = jo.optInt("ratings_count");
				}
			}

		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return result;

	}
	
	public void getRatingsCount(final AsyncCallback listener, final Object state) {

		new Thread() {

			public void run() {

				try {

					int[] result = new int[1];
					result[0] = getRatingsCountSync();

					try {
						listener.onComplete(result, state);
					} catch (Throwable t) {
						t.printStackTrace();
					}
				} catch (HttpClientException e) {
					listener.onHttpClientException(e, state);
				} catch (IOException e) {
					listener.onIOException(e, state);
				} catch (WebServiceException e) {
					listener.onWebServiceException(e, state);
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}.start();

	}
	
	public int getUserRating(String email) throws HttpClientException, IOException, WebServiceException {

		int result = 0;

		Hashtable<String, String> params = new Hashtable<String, String>();
		params.put("movieId", Integer.toString(getId()));
		params.put("email", email);

		try {

			JSONObject jo = read("movie/userrating", params);

			if ((jo != null) && (jo.length() > 0)) {
				if ((jo.has("rating"))) {
					result = jo.optInt("rating");
				}
			}

		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return result;

	}

	public void getUserRating(final String email, final AsyncCallback listener, final Object state) {

		new Thread() {

			public void run() {

				try {

					int[] result = new int[1];
					result[0] = getUserRating(email);

					try {
						listener.onComplete(result, state);
					} catch (Throwable t) {
						t.printStackTrace();
					}
				} catch (HttpClientException e) {
					listener.onHttpClientException(e, state);
				} catch (IOException e) {
					listener.onIOException(e, state);
				} catch (WebServiceException e) {
					listener.onWebServiceException(e, state);
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}.start();

	}

	public boolean rate(String email, int rating) throws HttpClientException, IOException, WebServiceException {
		
		boolean result = false;

		Hashtable<String, String> data = new Hashtable<String, String>();
		data.put("movieId", Integer.toString(getId()));
		data.put("email", email);
		data.put("rating", Integer.toString(rating));

		try {

			JSONObject jo = write("movie/rate", data);

			if ((jo != null) && (jo.length() > 0)) {
				if ((jo.has("message")) && (jo.optString("message").equalsIgnoreCase("success"))) {
					result = true;
				}
			}

		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return result;
		
	}

	public void rate(final String email, final int rating, final AsyncCallback listener, final Object state) {
		
		new Thread() {

			public void run() {

				try {
					boolean[] result = new boolean[1];
					result[0] = rate(email, rating);
					try {
						listener.onComplete(result, state);
					} catch (Throwable t) {
						t.printStackTrace();
					}
				} catch (HttpClientException e) {
					listener.onHttpClientException(e, state);
				} catch (IOException e) {
					listener.onIOException(e, state);
				} catch (WebServiceException e) {
					listener.onWebServiceException(e, state);
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}.start();
		
	}

}
